import Vue from 'vue'
import Router from 'vue-router'
 

Vue.use(Router)  

const router = new Router({
  routes: [
    {
      path: '/',
      name: 'index',
      component: ()=>import("@/views/home/index"),
      meta:{
      	requiresAuth: false,
        title:'首页'
      }
    },
    {
      path: '/login',
      name: 'login',
      component: ()=>import("@/views/home/login"),
      meta:{
        requiresAuth: false,
        title:'登录'
      }
    },
    {
      path: '/home',
      name: 'home',
      component: ()=>import("@/views/home/home"),
      meta:{
        requiresAuth: false,
        title:'内页首页'
      },
      children:[
        {
            path:'',
            name:'homeIndex',
            component: ()=>import("@/views/home/login_index"),
            meta:{
                requiresAuth:false
            }
        },
        {
            path:'/product',
            name:'product',
            component: ()=>import("@/views/product/product"),
            meta:{
                requiresAuth:false
            }
        },
        {
            path:'/release',
            name:'release',
            component: ()=>import("@/views/release/index"),
            meta:{
                requiresAuth:false
            }
        },
        {
            path:'/userCenter',
            name:'userCenter',
            component: ()=>import("@/views/userCenter/index"),
            meta:{
                requiresAuth:true
            }
        },
      ]
    },
    {
        path:'/productDetail',
        name:'productDetail',
        component: ()=>import("@/views/product/productDetail"),
        meta:{
            requiresAuth:false
        }
    },
    {
        path:'/propertyDetail',
        name:'propertyDetail',
        component: ()=>import("@/views/product/propertyDetail"),
        meta:{
            requiresAuth:false
        }
    },
    {
        path:'/releaseAdd',
        name:'releaseAdd',
        component: ()=>import("@/views/release/add"),
        meta:{
            requiresAuth:false
        }
    },
    {
        path:'/about',
        name:'about',
        component: ()=>import("@/views/userCenter/about"),
        meta:{
            requiresAuth:false
        }
    },
    
  ]
})

/*路由拦截器设置-before*/
router.beforeEach((to, from, next) => {
    // console.log(to,from) 
    let token = sessionStorage.getItem("token")
    //1.登录权限判断
    if(to.meta.requiresAuth) {
        //验证token，存在才跳转
        if(token) {
            next()
        } 
        else {
            next({
                path: "/login",
                query: { redirect: to.fullPath }
            })
        }
    } 
    else{ 
        next() 
    }
    /*2.非登陆条件下输入某个地址 需要先跳到登录页再跳转到相应页面*/
    // if(to.matched.length == 0 ){
    //     next({path: "/",query: { redirect: to.fullPath }})
    // }
});
// router.afterEach((to,from,next) => {
//   // console.log(to)
//   let path = to.path.split('/').slice(1)
//   // console.log(path)
//   let title = to.meta.title.split('/')
//   // console.log(title)
//   let arr = [{path:'/index',title:'首页'}]
//   if((path.length == 1) && (path[0]=='index' || path[0]=='about'  || path[0]=='introduce')){

//   }
//   else{
//       path.forEach((item,index)=>{
//         title.forEach((it,ind)=>{
//           if(index == ind){
//             arr.push({path:'/'+item,title:it})
//           }
//         })
//       })
//   }
  
//   // console.log(arr)
//   router.app.$store.commit("setbreadcrumbItems", arr);
// })

export default router